package week6;

class Resourcew {
    private String name;
    private int resourcew;

    Resourcew(String name, int resourcew) {
        this.name = name;
        this.resourcew = resourcew;
    }

    String getName() {
        return name;
    }

    synchronized int doSome() {
        return ++resourcew;
    }

    synchronized void cooperate(Resourcew resourcew) {
        resourcew.doSome();
        System.out.printf("%s 整合 %s 的資源%n",
                this.name, resourcew.getName());
    }
}

public class DeadLockDemo {
    public static void main(String[] args) {
        Resourcew resourcew1 = new Resourcew("resourcew1", 10);
        Resourcew resourcew2 = new Resourcew("resourcew2", 20);
        
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                resourcew1.cooperate(resourcew2);
            }
        });
        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                resourcew2.cooperate(resourcew1);
            }
        });

        thread1.start();
        thread2.start();
    }
}
